import pandas as pd
import pymysql

# 配置 MySQL 连接信息
host = 'localhost'
port = 3306
user = 'root'
password = '66655260'
database = 'myitem'

# 连接 MySQL 数据库
conn = pymysql.connect(
    host=host,
    port=port,
    user=user,
    password=password,
    database=database
)

# 读取 Excel 文件
df = pd.read_excel('java.xlsx')

# 将数据插入到 MySQL 数据库中
for i, row in df.iterrows():
    question = row['题目']
    answer = row['答案']
    question_type = row['类型']
    source = row['来源']
    difficulty = row['难度']
    bank_type = row['题库类型']

    # 执行插入操作
    with conn.cursor() as cursor:
        sql = 'INSERT INTO interviewqas (question, answer, question_type, bank_type, source, difficulty) VALUES (%s, %s, %s, %s, %s, %s)'
        cursor.execute(sql, (question, answer, question_type, bank_type, source, difficulty))
    conn.commit()

# 关闭 MySQL 连接
conn.close()
